<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>快速开始 | vue-element-dict</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/vue-element-dict/favicon.ico">
    <script defer="defer" async="async" src="https://cpro.baidustatic.com/cpro/ui/cm.js"></script>
    <meta name="description" content="vue-element-dict字典包官方使用文档">
    
    <link rel="preload" href="/vue-element-dict/assets/css/0.styles.2f302933.css" as="style"><link rel="preload" href="/vue-element-dict/assets/js/app.796ae7e4.js" as="script"><link rel="preload" href="/vue-element-dict/assets/js/4.df041e95.js" as="script"><link rel="preload" href="/vue-element-dict/assets/js/37.dc841a6a.js" as="script"><link rel="prefetch" href="/vue-element-dict/assets/js/1.9df3603b.js"><link rel="prefetch" href="/vue-element-dict/assets/js/10.55ebc9f1.js"><link rel="prefetch" href="/vue-element-dict/assets/js/11.ae1602ab.js"><link rel="prefetch" href="/vue-element-dict/assets/js/12.7a6d05ba.js"><link rel="prefetch" href="/vue-element-dict/assets/js/13.1d9a30ea.js"><link rel="prefetch" href="/vue-element-dict/assets/js/14.83cfdb89.js"><link rel="prefetch" href="/vue-element-dict/assets/js/15.c09cab0b.js"><link rel="prefetch" href="/vue-element-dict/assets/js/16.19c4ed7e.js"><link rel="prefetch" href="/vue-element-dict/assets/js/17.5c7fb955.js"><link rel="prefetch" href="/vue-element-dict/assets/js/18.b211f0fa.js"><link rel="prefetch" href="/vue-element-dict/assets/js/19.aa88b046.js"><link rel="prefetch" href="/vue-element-dict/assets/js/2.eb833b2e.js"><link rel="prefetch" href="/vue-element-dict/assets/js/20.740e71a2.js"><link rel="prefetch" href="/vue-element-dict/assets/js/21.814870f8.js"><link rel="prefetch" href="/vue-element-dict/assets/js/22.46503405.js"><link rel="prefetch" href="/vue-element-dict/assets/js/23.87118240.js"><link rel="prefetch" href="/vue-element-dict/assets/js/24.ac6635e4.js"><link rel="prefetch" href="/vue-element-dict/assets/js/25.f678ea41.js"><link rel="prefetch" href="/vue-element-dict/assets/js/26.d8cc5201.js"><link rel="prefetch" href="/vue-element-dict/assets/js/27.7078c96d.js"><link rel="prefetch" href="/vue-element-dict/assets/js/28.57b6222a.js"><link rel="prefetch" href="/vue-element-dict/assets/js/29.010701c9.js"><link rel="prefetch" href="/vue-element-dict/assets/js/30.c6e653d6.js"><link rel="prefetch" href="/vue-element-dict/assets/js/31.d1d5da99.js"><link rel="prefetch" href="/vue-element-dict/assets/js/32.6f3739df.js"><link rel="prefetch" href="/vue-element-dict/assets/js/33.d87a4b44.js"><link rel="prefetch" href="/vue-element-dict/assets/js/34.3eca72ee.js"><link rel="prefetch" href="/vue-element-dict/assets/js/35.0a8dc5d9.js"><link rel="prefetch" href="/vue-element-dict/assets/js/36.121eaca2.js"><link rel="prefetch" href="/vue-element-dict/assets/js/38.70886628.js"><link rel="prefetch" href="/vue-element-dict/assets/js/5.a10e50db.js"><link rel="prefetch" href="/vue-element-dict/assets/js/6.10b21df8.js"><link rel="prefetch" href="/vue-element-dict/assets/js/7.b647619a.js"><link rel="prefetch" href="/vue-element-dict/assets/js/8.4a0866ed.js"><link rel="prefetch" href="/vue-element-dict/assets/js/9.9ff5451c.js">
    <link rel="stylesheet" href="/vue-element-dict/assets/css/0.styles.2f302933.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vue-element-dict/" class="home-link router-link-active"><!----> <span class="site-name">vue-element-dict</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/vue-element-dict/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/vue-element-dict/guide/" class="nav-link router-link-active">
  指南
</a></div><div class="nav-item"><a href="https://www.npmjs.com/package/vue-element-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  npm地址
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/vue-element-dict/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/vue-element-dict/guide/" class="nav-link router-link-active">
  指南
</a></div><div class="nav-item"><a href="https://www.npmjs.com/package/vue-element-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  npm地址
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-element-dict/guide/" aria-current="page" class="sidebar-link">介绍</a></li><li><a href="/vue-element-dict/guide/fast/" aria-current="page" class="active sidebar-link">快速开始</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vue-element-dict/guide/fast/#安装" class="sidebar-link">安装</a></li><li class="sidebar-sub-header"><a href="/vue-element-dict/guide/fast/#项目实践" class="sidebar-link">项目实践</a></li><li class="sidebar-sub-header"><a href="/vue-element-dict/guide/fast/#对于嵌套iframe的项目" class="sidebar-link">对于嵌套iFrame的项目</a></li></ul></li><li><a href="/vue-element-dict/guide/directory-structure/" class="sidebar-link">目录结构</a></li><li><a href="/vue-element-dict/guide/config/" class="sidebar-link">字典包配置</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>普通字典组件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-element-dict/components/el-select-dict/" class="sidebar-link">select-dict选择器</a></li><li><a href="/vue-element-dict/components/el-radio-dict/" class="sidebar-link">radio-dict单选框</a></li><li><a href="/vue-element-dict/components/el-checkbox-dict/" class="sidebar-link">checkbox-dict多选框</a></li><li><a href="/vue-element-dict/components/el-radio-button-dict/" class="sidebar-link">radio-button-dict单选框</a></li><li><a href="/vue-element-dict/components/el-checkbox-button-dict/" class="sidebar-link">checkbox-button-dict多选框</a></li><li><a href="/vue-element-dict/components/el-tabs-dict/" class="sidebar-link">tabs-dict标签页</a></li><li><a href="/vue-element-dict/components/el-tag-dict/" class="sidebar-link">tag-dict标签</a></li><li><a href="/vue-element-dict/components/el-link-dict/" class="sidebar-link">link-dict链接</a></li><li><a href="/vue-element-dict/components/el-button-dict/" class="sidebar-link">button-dict链接</a></li><li><a href="/vue-element-dict/components/el-table-colmun-dict/" class="sidebar-link">el-table-colmun-dict表格</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>树形字典组件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-element-dict/components/el-cascader-dict/" class="sidebar-link">cascader-dict联级选项</a></li><li><a href="/vue-element-dict/components/el-tree-dict/" class="sidebar-link">tree-dict树菜单</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>其他组件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-element-dict/components/el-cascader-new/" class="sidebar-link">cascader-new联级选项</a></li><li><a href="/vue-element-dict/components/el-tree-new/" class="sidebar-link">tree-new树菜单</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>过滤器</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-element-dict/filters/dict/" class="sidebar-link">字典过滤器</a></li><li><a href="/vue-element-dict/filters/tree-dict/" class="sidebar-link">树形字典过滤器</a></li><li><a href="/vue-element-dict/filters/other/" class="sidebar-link">其他过滤器</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>方法</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-element-dict/functions/dict/" class="sidebar-link">字典方法</a></li><li><a href="/vue-element-dict/functions/tree-dict/" class="sidebar-link">树形字典方法</a></li><li><a href="/vue-element-dict/functions/other/" class="sidebar-link">其他方法</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="快速开始"><a href="#快速开始" class="header-anchor">#</a> 快速开始</h1> <p>此篇文章教您如何快速使用vue-element-dict包，让您从入门到熟练。下文默认您已经按结构目录创建好文件。<a href="/vue-element-dict/guide/directory-structure/">目录结构&gt;&gt;</a></p> <h2 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h2> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> -S vue-element-dict
</code></pre></div><h2 id="项目实践"><a href="#项目实践" class="header-anchor">#</a> 项目实践</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//main.js 文件</span>

<span class="token comment">//引入element的css 如果已引入 可不要重复引入</span>
<span class="token keyword">import</span> <span class="token string">&quot;element-ui/lib/theme-chalk/index.css&quot;</span>
<span class="token comment">//获取字典数据版本号接口  接口名自定义</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span>getGlobalConfigApi<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;@/api/module/common-api.js&quot;</span>
<span class="token comment">//字典包配置文件</span>
<span class="token keyword">import</span> dictSetting <span class="token keyword">from</span> <span class="token string">&quot;@/dict-setting.js&quot;</span>
<span class="token keyword">const</span> vueElementDict <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;vue-element-dict&quot;</span><span class="token punctuation">)</span>

<span class="token comment">//获取接口返回的全局配置（包含当前字典版本号）</span>
<span class="token function">getGlobalConfigApi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">data</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span>verssion<span class="token punctuation">}</span> <span class="token operator">=</span> data <span class="token comment">//获取版本号</span>
  <span class="token comment">//合并字典包配置</span>
  Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>dictSetting<span class="token punctuation">,</span> <span class="token punctuation">{</span>verssion<span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token comment">//安装字典包</span>
  Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>vueElementDict<span class="token punctuation">,</span> dictSetting<span class="token punctuation">)</span>
  <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token function-variable function">render</span><span class="token operator">:</span> <span class="token parameter">h</span> <span class="token operator">=&gt;</span> <span class="token function">h</span><span class="token punctuation">(</span>App<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">$mount</span><span class="token punctuation">(</span><span class="token string">&quot;#app&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>对于项目经常使用到的字典类型，可配置 usuallyGetFileds 字段 详见<a href="/vue-element-dict/guide/config/">字典包配置&gt;&gt;</a></p></div> <!----> <h2 id="对于嵌套iframe的项目"><a href="#对于嵌套iframe的项目" class="header-anchor">#</a> 对于嵌套iFrame的项目</h2> <p>main.js文件同上</p> <p>在路由拦截处书写代码类似如下</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token punctuation">{</span>getDictObjByDictTypes<span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;@/utils/vue-element-dict.umd.min.js&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// 演示替换到维修升级页面</span>
<span class="token keyword">function</span> <span class="token function">beforeEachTo</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">/**
  **下面的page1 page2 page3 为路由的name属性值， typeXX 为该页面引用到的字典类型
  **此段代码目的：防止页面上的字典组件单词请求一个字典接口，导致请求过多接口，影响性能，进页面前一次性获取
  **此获取数据接口只 请求缓存中不存在的 字典数据值，几乎不影响性能，可放心使用
  **如页面较多，可新开页面书写此段代码
  **对于非iFrame项目，也可如此处理
  **/</span>
  <span class="token keyword">const</span> getDictType <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">page1</span><span class="token operator">:</span> <span class="token string">&quot;type11,type12,type13,type14&quot;</span><span class="token punctuation">,</span>
    <span class="token literal-property property">page2</span><span class="token operator">:</span> <span class="token string">&quot;type21,type22,type23,type24&quot;</span><span class="token punctuation">,</span>
   	<span class="token literal-property property">page3</span><span class="token operator">:</span> <span class="token string">&quot;type31,type32,type33,type34&quot;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>getDictType<span class="token punctuation">[</span>to<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">getDictObjByDictTypes</span><span class="token punctuation">(</span>getDictType<span class="token punctuation">[</span>to<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token punctuation">{</span>
  beforeEachTo
<span class="token punctuation">}</span>
</code></pre></div><div class="custom-block tip"><p class="custom-block-title">TIP</p> <ol><li>上述代码的page1 page2 page3 为路由的name属性值， typeXX 为该页面引用到的字典类型</li> <li>此段代码目的：防止页面上的字典组件单词请求一个字典接口，导致请求过多接口，影响性能，进页面前一次性获取</li> <li>此获取数据方法只 请求缓存中不存在的 字典数据值，几乎不影响性能，可放心使用</li> <li>如页面较多，可新开页面书写此段代码</li> <li>对于非iFrame项目，也可如此处理</li></ol></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">6/29/2022, 3:01:11 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/vue-element-dict/guide/" class="prev router-link-active">
        介绍
      </a></span> <span class="next"><a href="/vue-element-dict/guide/directory-structure/">
        目录结构
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><div id="loading-mask"><div class="loading-wrapper"><span class="loading-dot loading-dot-spin"><i></i> <i></i> <i></i> <i></i></span></div></div><!----></div></div>
    <script src="/vue-element-dict/assets/js/app.796ae7e4.js" defer></script><script src="/vue-element-dict/assets/js/4.df041e95.js" defer></script><script src="/vue-element-dict/assets/js/37.dc841a6a.js" defer></script>
  </body>
</html>
